﻿var c_CommentForm;
var c_SubmitBtn;
var c_MsgSpan;
var c_CommentPanel;
var c_RegexId;
var c_AuthorKeywordSpan;
var c_Visitor;
var c_UserKeywordSpan;
var c_AddKeywordsForm;
var dlg_AddKeywordsDlg = null;

function GetElements()
{
	c_CommentForm = YAHOO.util.Dom.get("CommentForm");
	c_SubmitBtn = YAHOO.util.Dom.get("SubmitBtn");
	c_MsgSpan = YAHOO.util.Dom.get("MsgSpan");
	c_CommentPanel = YAHOO.util.Dom.get("CommentPanel");
	c_Visitor = YAHOO.util.Dom.get("Visitor");
	c_RegexId = YAHOO.util.Dom.get("RegexId");
	c_AuthorKeywordSpan = YAHOO.util.Dom.get("AuthorKeywordSpan");
	try{c_UserKeywordSpan = YAHOO.util.Dom.get("UserKeywordSpan");}catch(e){c_UserKeywordSpan = null;}
	c_AddKeywordsForm = YAHOO.util.Dom.get("AddKeywordsForm");
}

function SetErrMsg(msg)
{
	c_MsgSpan.className = "err";
	c_MsgSpan.innerText = msg;
}

function SetSucMsg(msg)
{
	c_MsgSpan.className = "suc";
	c_MsgSpan.innerText = msg;
}

function DoSubmitComment()
{
	c_SubmitBtn.disabled = true;

	var callback =
	{
		success: function(o)
		{
			try
			{
				var data = YAHOO.lang.JSON.parse(o.responseText);
				
				if(data.IsError)
				{
					SetErrMsg(data.Message);
				}
				else
				{
					c_CommentForm.reset();
					SetSucMsg(data.Message);
					GetComments(-1);
				}
			}
			catch(e)
			{
				SetErrMsg("解析响应时出错，请稍后再试。");
			}
			
			c_SubmitBtn.disabled = false;
		},
		failure: function(o)
		{
			SetErrMsg("连接错误。");
			c_SubmitBtn.disabled = false;
		},
		timeout:30000
	};
	YAHOO.util.Connect.setForm(c_CommentForm);
	YAHOO.util.Connect.asyncRequest("POST", "/Regex/CommentSubmit/", callback, null);
}

function ShowComments(data)
{
	while(c_CommentPanel.hasChildNodes())
		c_CommentPanel.removeChild(c_CommentPanel.firstChild);
	for(var i in data.Comments)
	{
		var c = data.Comments[i];
		var pt = document.createElement("p");
		var pb = document.createElement("p");
		var phr = document.createElement("hr");
		
		pt.innerHTML = "[" + c.CommentAddedTime + "] <a href='/User/" + c.UserAccessKey + "/'>" + c.UserNickname + "</a>：";
		pb.innerText = c.CommentBody;
		
		c_CommentPanel.appendChild(pt);
		c_CommentPanel.appendChild(pb);
		c_CommentPanel.appendChild(phr);
	}
	
	var ppg = document.createElement("p");
	for(var pg = 1; pg <= data.TotalPages; pg++)
	{
		var psp = document.createElement("span");
		if(pg == data.PageIndex)
		{
			psp.className = "rx-curpg";
			psp.innerText = pg;
		}
		else
		{
			psp.className = "rx-pg";
			var pga = document.createElement("a");
			pga.href = "javascript:GetComments(" + pg + ")";
			pga.innerText = pg;
			psp.appendChild(pga);
		}
		ppg.appendChild(psp);
	}
	c_CommentPanel.appendChild(ppg);
}

function GetComments(pg)
{
	var pageSize = 20;
	var svrurl = "/Regex/GetComments/?regexId=" + c_RegexId.value + "&pageIndex=" + pg + "&pageSize=" + pageSize + "&p=" + Math.random();
	
	var callback =
	{
		success: function(o)
		{
			try
			{
				var data = YAHOO.lang.JSON.parse(o.responseText);
				
				if(data.IsError)
				{
					alert(data.Message);
				}
				else
				{
					ShowComments(data);
				}
			}
			catch(e)
			{
				alert("解析响应时出错，请稍后再试。" + e.message);
			}
		},
		failure: function(o)
		{
			alert("连接错误。" + o.statusText);
		},
		timeout:30000
	};
	
	YAHOO.util.Connect.asyncRequest("GET", svrurl, callback, null);
}

function doAddKeywords()
{
	if(dlg_AddKeywordsDlg == null)
		return;
		
	dlg_AddKeywordsDlg.show();
}

function ShowKeywords(ks)
{
	var kspan = c_Visitor.value == "author" ? c_AuthorKeywordSpan : c_UserKeywordSpan;
	kspan.innerHTML = "";
	if(ks.length == 0)
	{
		kspan.innerHTML = "暂无。 ";
		return;
	}
	for(var i = 0; i < ks.length; i++)
	{
		var kurl = "/Keyword/" + encodeURIComponent(ks[i].Text) + "/";
		kspan.innerHTML += "<a href='" + kurl + "'>" + ks[i].Text + "</a> <a href='javascript:DelKeyword(\"" + ks[i].Id + "\");' title='删除'><img src='/Content/images/del.gif' alt='删除' /></a> ";
		if(i < ks.length - 1)
			kspan.innerHTML += " | ";
	}
}

function GetKeywords()
{
	var svrurl="/Regex/GetKeywords/?regexId=" + c_RegexId.value + "&p=" + Math.random();
	var callback=
	{
		success: function(o)
		{
			var data = YAHOO.lang.JSON.parse(o.responseText);
			if(!data.IsError)
				ShowKeywords(data.Keywords);
		},
		timeout: 30000
	};
	
	YAHOO.util.Connect.asyncRequest("GET", svrurl, callback, null);
}

function DelKeyword(id)
{
	if(!confirm("确定要删除该关键字吗？"))
		return;

	var svrurl = "/Regex/DeleteKeyword/?regexId=" + c_RegexId.value + "&keywordId=" + id + "&p=" + Math.random();
	var callback =
	{
		success: function(o){GetKeywords();},
		failure: function(o){alert("连接错误，请稍后再试。");},
		timeout: 30000
	};
	
	YAHOO.util.Connect.asyncRequest("GET", svrurl, callback, null);
}

function CreateAddKeywordsDialog()
{
	dlg_AddKeywordsDlg = new YAHOO.widget.Dialog("AddKeywordsDialog",
	{
		width:"300px",
		fixedcenter:true,
		visible:false,
		constraintoviewport:true,
		modal:true,
		buttons:[{text:"确定",isDefault:true,handler:function(){this.submit();}},
			{text:"取消",handler:function(){this.cancel();}}]
	});
	dlg_AddKeywordsDlg.callback =
	{
		success: function(o)
		{
			c_AddKeywordsForm.reset();
			GetKeywords();
		},
		failure: function(o)
		{
			alert("连接错误，请稍后再试。");
		}
	};
	dlg_AddKeywordsDlg.render();
}

function init()
{
	GetElements();
	CreateAddKeywordsDialog();
	YAHOO.util.Event.addListener(c_SubmitBtn, "click", DoSubmitComment);
	GetComments(1);
}

YAHOO.util.Event.onDOMReady(init);
